﻿function LoadRegionList(loadingtype, layername, _layer)
{
    var _parentid = "";
    if(layername == "province")
        _parentid = "0";   
    if(layername == "city")
        _parentid = $("#selProvinceList").val(); 
    if(layername == "countie")
        _parentid = $("#selCityList").val();
        
    if(_parentid != "")
    {
        $.ajax({
            async   :   false,
            type    :   "POST",
            url     :   prependOrgName("/shopping_orderutility.aspx"),
            data    :   { otype : "getregionlist", layer: _layer, parentid : _parentid },
            success :   function(data, textStatus) {
                switch(_layer)
                {
                    case 1:
                        $("#selProvinceList").emptySelect().append(data).change(function() {
                            $("#txtProvinceId").val($("#selProvinceList").val());
                            $("#txtCityId").val($("#selCityList").val()); 
                            LoadRegionList("event", "city", 2);
                            LoadRegionList("event", "countie", 3);
                        });
                        break;
                    case 2:
                        $("#selCityList").emptySelect().append(data).change(function() {
                            $("#txtCityId").val($("#selCityList").val()); 
                            LoadRegionList("event", "countie", 3); 
                        });
                        break;
                    case 3:
                        $("#selCountiesList").emptySelect().append(data).change(function() { 
                            $("#txtCountieId").val($("#selCountiesList").val()); 
                        }); 
                        break;
                }
            }
        });

        if(loadingtype == "init")
        {
            if($("#txtProvinceId").val() != "")
                $("#selProvinceList").val($("#txtProvinceId").val());
            if($("#txtCountieId").val() != "")
                $("#selCountiesList").val($("#txtCountieId").val());
            if($("#txtCityId").val() != "")
                $("#selCityList").val($("#txtCityId").val());
        }
    }
    else
    {
        if($("#txtProvinceId").val() == "")
        {
            $("#selCityList").emptySelect().append("<option value=''>-- 请选择城市 --</option>");
            $("#txtProvinceId").val("");
            $("#txtCityId").val("");
            $("#txtCountieId").val("");
        }
        if($("#txtCityId").val() == "")
        {
            $("#selCountiesList").emptySelect().append("<option value=''>-- 请选择区县 --</option>");
            $("#txtCityId").val("");
            $("#txtCountieId").val("");
        }
    }
}
/// 送货地址——匹配ID初始化数据
function InitializeAddressObj()
{
    var values = null;
    if ($("#txtShippingAddressId").val() != "") {
        $.ajax({
            async: false,
            type: "POST",
            url: prependOrgName("/shopping_orderutility.aspx"),
            data: {
                otype: "getaddressobj",
                txtShippingAddressId: $("#txtShippingAddressId").val()
            },
            success: function (data, textStatus) {
                values = data;
            }
        });
    }
    return values;
}

/// 送货地址——加载用户所有地址
function LoadAddressNameList()
{
    $.ajax({
        async   :   false,
        type    :   "POST",
        url:  prependOrgName("/shopping_orderutility.aspx"),
        data    :   { otype : "getaddresslist" },
        success :   function(data, textStatus) {
            $("#selShippingAddressList").emptySelect().append(data).change(function() {
                $("#txtShippingAddressId").val($("#selShippingAddressList").val());
                GetAddressContentEditInfo();
            });
        }
    });
}
/// 送货地址——初始化显示内容
function GetAddressContentInfo()
{
    var _values = InitializeAddressObj(); 
    $(_values).find("filed").each(function(){
        if($("#span"+$(this).attr("name")))
        {
            $("#span"+$(this).attr("name") + "_Info").html($(this).text());
            $("#hid"+$(this).attr("name")).val($(this).text());
        }
    })
}
/// 送货地址——初始化编辑内容
function GetAddressContentEditInfo() { 
    var _values = InitializeAddressObj(); 
    $(_values).find("filed").each(function(){
        if($("#txt"+$(this).attr("name")))
            $("#txt"+$(this).attr("name")).val($(this).text());
    })
}
/// 送货地址——切换地址编辑模式
function ShippingAddressEdit() { 
    if ($("#txtShippingAddressId").val() != "") {
        $("#shippingAddressContentEditPanel").css("display", "");
        $("#shippingAddressContentInfo").css("display", "none");
        $("#firstLine").css("display", "");
        $("#btnAddShippingInfo").css("display", "none");
        $("#btnEditShippingInfo").css("display", "");
        $("#liAddressTypeInput").css("display", "none");

        /// 初始化编辑内容
        GetAddressContentEditInfo();
        /// 绑定当前关联地址
        $("#selShippingAddressList").val($("#txtShippingAddressId").val());
        /// 加载地区选择列表
        if ($("#txtProvinceId").val() != "")
            $("#selProvinceList").val($("#txtProvinceId").val());
    }
    else {
        $("#shippingAddressContentEditPanel").css("display", "");
        $("#shippingAddressContentInfo").css("display", "none");
        $("#firstLine").css("display", "");
        $("#btnAddShippingInfo").css("display", "none");
        $("#btnEditShippingInfo").css("display", "");
        $("#liAddressTypeInput").css("display", "none");

        $("#firstLine").css("display", "none"); 
        $("#liAddressTypeInput").css("display", "");
        $("#btnAddShippingInfo").css("display", "");
        $("#btnEditShippingInfo").css("display", "none");
        ClearAddressControlsValues();
    }
    LoadRegionList('init', 'province', 1);
    LoadRegionList('init', 'city', 2);
    LoadRegionList('init', 'countie', 3);
}
/// 送货地址——切换地址添加模式
function AddShippingAddress()
{
    $("#firstLine").css("display", "none");

    $("#liAddressTypeInput").css("display", "");
    $("#btnAddShippingInfo").css("display", "");
    $("#btnEditShippingInfo").css("display", "none");
    ClearAddressControlsValues();
}
/// 送货地址——取消编辑模式
function ShippingAddressCancel()
{
    $("#shippingAddressContentEditPanel").css("display", "none");
    $("#shippingAddressContentInfo").css("display", ""); 
}
/// 送货地址——清空编辑控件值
function ClearAddressControlsValues()
{ 
    $("#txtShipAddressName").val("");
    $("#txtShipToName").val("");
    $("#txtMobile").val("");
    $("#txtShipTel").val("");
    $("#txtShipAddress").val("");
    $("#txtShipZip").val("");
    $("#txtShipTel").val("");
    $("#txtShipMobile").val(""); 
    $("#selProvinceList").val("");
    $("#selCountiesList").emptySelect().append("<option value=''>-- 请选择城市 --</option>");
    $("#selCityList").emptySelect().append("<option value=''>-- 请选择区县 --</option>");
    $("#txtProvinceId").val("");
    $("#txtCityId").val(""); 
    $("#txtCountieId").val("");
}
/// 送货地址——保存编辑内容 addshippingaddress / modifyshippingaddress
function ShippingAddressSave(_otype)
{
    $.ajax({
        async: true,
        type: "POST",
        url:  prependOrgName("/shopping_orderutility.aspx"),
        data: {
            otype:                  _otype,
            txtShippingAddressId:   $("#txtShippingAddressId").val(),
            txtShipAddressName:     $("#txtShipAddressName").val(),
            txtShipToName:          $("#txtShipToName").val(),
            txtMobile:              $("#txtMobile").val(),
            txtShipTel:             $("#txtShipTel").val(),
            txtShipAddress:         $("#txtShipAddress").val(),
            txtShipCounties:        GetDrpListText("selCountiesList"),
            txtShipCity:            GetDrpListText("selCityList"),
            txtShipState:           GetDrpListText("selProvinceList"),
            txtShipZip:             $("#txtShipZip").val(),
            txtShipTel:             $("#txtShipTel").val(),
            txtShipMobile:          $("#txtShipMobile").val(), 
            txtShipCountiesId:      $("#selCountiesList").val(),
            txtShipCityId:          $("#selCityList").val(),
            txtShipStateId:         $("#selProvinceList").val()
        },
        beforeSend: function() { 
            $("#loading1").css("display", "");
        },
        success: function(data, textStatus) {
            if(data.split('|')[0] == "succeeded")
            {
                alert("保存成功！");
                $("#loading1").css("display", "none"); 
                if(document.getElementById("shippingAddressContentInfo")) {
                    $("#txtShippingAddressId").val(data.split('|')[1])
                    $("#shippingAddressContentEditPanel").css("display", "none");
                    $("#shippingAddressContentInfo").css("display", "");
                    GetAddressContentInfo();
                    LoadAddressNameList();
                }
                else
                {
                    window.location.href="shippingaddresslist.aspx";
                }
            }
            else
            {
                alert(data);
            }
        }
    });
}
function LoadingOrderItems(tagObjectId, _orderid)
{
    $.ajax({
        async: true,
        type: "POST",
        url:  prependOrgName("/shopping_orderutility.aspx"),
        data: {
            otype: "loadingorderitems",
            orderid: _orderid
        },
        beforeSend: function() {
            $("#"+tagObjectId).append("<tr><td colspan=\"4\" style=\"height: 50px; text-align: center;\"><img src=" + prependOrgName("/_imgs/progress.gif") + " style=\"border: 0;\" /></td></tr>");
            $("#"+tagObjectId).html("");
        },
        success: function(data, textStatus) { 
            $("#"+tagObjectId).html("");
            $("#"+tagObjectId).append(data);
            TotalOrderAmount(_orderid);
        }
    });
}


var _PageIndex;
var _PageUpCount;
var _Direction;
function LoadingOrderList(tagObjectId)
{
    var _querytext = $("#txtSearch").val();
    if (_querytext == $("#txtSearch").attr("init_value"))
        _querytext = "";
    
    $.ajax({
        async: true,
        type: "POST",
        url: prependOrgName("/shopping_orderutility.aspx"),
        data: {
            otype:          "loadingorderilist", 
            page:           _PageIndex,
            upcount:        _PageUpCount,
            direction:      _Direction,
            querytext:      _querytext,
            status: $("#selStatus").val()
        },
        beforeSend: function() {
            $("#" + tagObjectId).append("<tr><td colspan=\"5\" style=\"height: 50px; text-align: center;\"><img src=" + prependOrgName("/_imgs/progress.gif") + " style=\"border: 0;\" /></td></tr>");
            $("#" + tagObjectId).html("");
        },
        success: function(data, textStatus) {  
            $("#" + tagObjectId).html("");
            $("#" + tagObjectId).append(data); 
        }
    });
}
function pagination(page, upcount, direction) { 
    _PageIndex=page;
    _PageUpCount=upcount;
    _Direction=direction;
    LoadingOrderList("tbdOrderList");
}
function QueryOrderList()
{
    _PageIndex=1;
    _PageUpCount=1;
    _Direction="";
    LoadingOrderList("tbdOrderList");
}

function LoadingOrderListForIndex(tagObjectId) { 
    $.ajax({
        async: true,
        type: "POST",
        url: prependOrgName("/shopping_orderutility.aspx"),
        data: {
            otype: "loadingorderilistforindex"
        },
        beforeSend: function () {
            $("#" + tagObjectId).append("<tr><td colspan=\"5\" style=\"height: 50px; text-align: center;\"><img src=" + prependOrgName("/_imgs/progress.gif") + " style=\"border: 0;\" /></td></tr>");
            $("#" + tagObjectId).html("");
        },
        success: function (data, textStatus) {
            $("#" + tagObjectId).html("");
            $("#" + tagObjectId).append(data);
        }
    });
}

function UpdateOrderQty(_obj)
{
    if(Trim($("#hidQty"+_obj.pid).val()) == _obj.value)
        return false;
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_orderutility.aspx"),
        data    :   { otype: "updateorderitemqty", _itemid : _obj.itemid, qty : _obj.value },
        async   :   false,
        success :   function(data, textStatus){
            if(data == "succeeded"){
                $("#hidQty"+_obj.pid).val(_obj.value);
                $("#changeQtyText"+_obj.pid).fadeIn();
                var _subtotal = parseFloat(Trim(_obj.value)) * parseFloat(Trim($("#spanPrice"+_obj.pid).text())); 
                $("#spanSubtotal"+_obj.pid).html(String(_subtotal));
                TotalOrderAmount(_obj.itemid.split('|')[1]);
                $("#changeQtyText"+_obj.pid).fadeOut();
            }
            else {
                alert(data);
            }
        }
    });
}

function RemoveOrderItem(_obj, tagObjectId)
{
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_orderutility.aspx"),
        data    :   { otype: "removeorderitem", _itemid : _obj.itemid },
        async   :   false,
        success :   function(data, textStatus){
            if(data == "succeeded"){
                 LoadingOrderItems(tagObjectId, _obj.orderid);
                 TotalOrderAmount(_obj.orderid);
            }
            else {
                alert(data);
            }
        }
    });
}

function TotalOrderAmount(_orderid)
{
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_orderutility.aspx"),
        data    :   { otype: "totalorderamount", orderid : _orderid },
        async   :   false,
        success :   function(data, textStatus){
            $("#emTotalCost").html("￥" + String(data));
            $("#txtTotalCost").val(String(data)); 
        }
    });
}